package com.sunny.blog.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sunny.blog.pojo.IpInfoBean;
import com.sunny.blog.service.OpenApiService;
import com.sunny.blog.service.SmsSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @ClassName: OpenApiServiceImpl
 * @Description:
 * @Author: sunt
 * @Date: 2020/5/9 9:47
 * @Version 1.0
 **/
@Service
@Slf4j
public class OpenApiServiceImpl implements OpenApiService {

    @Value("${query.ip_url}")
    private String ipQueryUrl;
    @Value("${baidu.push_url}")
    private String pushUrl;
    @Autowired
    private SmsSendService smsSendService;

    @Override
    public IpInfoBean queryIpInfo(String ip) {
        log.info("--------------->当前访问IP:{}，查询IPurl:{}",ip,ipQueryUrl + "&ip=" + ip);
        String ipInfo = HttpUtil.get(ipQueryUrl + "&ip=" + ip);
        IpInfoBean ipInfoBean = JSONUtil.toBean(ipInfo, IpInfoBean.class);
        return ipInfoBean;
    }

    @Override
    public void baiduPush(String site) {
        try {
            String post = HttpUtil.post(pushUrl, site);
            JSONObject jsonObject = JSONUtil.parseObj(post);
            String remain = jsonObject.getStr("remain");
            String success = jsonObject.getStr("success");
            String content = "文章链接:" + site + "，推送成功，今日剩余的可推送url条数:" + remain + ",当前成功推送条数:" + success;
            smsSendService.sendDingTextMsg(content,null,true);
        } catch (Exception e) {
            log.error("-------->推送异常" + e.getMessage());
            e.printStackTrace();
        }


    }

}
